package com.mxpio.erp.equipment.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepDetailParam;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepDetail;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepDetailService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;


@Tag(name = "EquipmentUpkeepDetailController}", description = "保养类别")
@RestController
@RequestMapping("/erp/equipment/upkeep/category/detail/")
public class EquipmentUpkeepDetailController {

    @Autowired
    private EquipmentUpkeepDetailService equipmentUpkeepDetailService;


    @GetMapping("list")
    @Operation(summary = "保养类别列表", description = "获取保养类别列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepDetail>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepDetail> list = equipmentUpkeepDetailService.list(EquipmentUpkeepDetail.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "保养类别列表", description = "获取保养类别列表（分页）", method = "GET")
    public Result<Page<EquipmentUpkeepDetail>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentUpkeepDetail> pageResult = equipmentUpkeepDetailService.listPage(EquipmentUpkeepDetail.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取保养类别列表", description = "根据ID获取保养类别列表", method = "GET")
    public Result<EquipmentUpkeepDetail> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentUpkeepDetail equipmentUpkeepDetail = equipmentUpkeepDetailService.getById(EquipmentUpkeepDetail.class, id);
        return Result.OK(equipmentUpkeepDetail);
    }

    @RequestMapping("add")
    @Operation(summary = "保存保养类别", description = "保存保养类别")
    public Result<EquipmentUpkeepDetail> add(@RequestBody EquipmentUpkeepDetail equipmentUpkeepDetail) {
        equipmentUpkeepDetailService.saveFromParams(equipmentUpkeepDetail);
        return Result.OK(equipmentUpkeepDetail);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑保养类别", description = "编辑保养类别", method = "PUT")
    public Result<EquipmentUpkeepDetail> edit(@RequestBody EquipmentUpkeepDetail equipmentUpkeepDetail) {
        equipmentUpkeepDetailService.editFromParams(equipmentUpkeepDetail);
        return Result.OK(equipmentUpkeepDetail);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除保养类别", description = "删除保养类别,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentUpkeepDetail> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentUpkeepDetailService.delete(EquipmentUpkeepDetail.class, key, new SmartCrudPolicyAdapter() {
                @Override
                public boolean beforeDelete(CrudContext context) {
                    Object o = context.getEntity();
                    if (o instanceof EquipmentUpkeepDetail) {
                        try{
                            JpaUtil.lind(EquipmentUpkeepDetailParam.class).equal("detailId", ((EquipmentUpkeepDetail) o).getId()).delete();
                        }catch (Exception e){
                            return false;
                        }
                    }
                    return true;
                }
            });
        }
        return Result.OK();
    }


}
